
import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/21 11:53
 * @description 层序遍历 BFS
 */
public class Solution {
    public static void main(String[] args) {

    }

    public int maxLevelSum(TreeNode root) {
        int cur = 1, sum = Integer.MIN_VALUE, ans = cur;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            int nSum = 0;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                nSum += node.val;
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            if (nSum > sum) {
                ans = cur;
                sum = nSum;
            }
            cur++;
        }
        return ans;
    }
}
